import config from '../config/config.js'
import des from '../util/desHeader.js'
export default{
	//封装uni.request()：
	request(options){
		return new Promise((resolve,reject)=>{
			//书写异步操作的代码
			uni.request({
				...options,
				success:res=>{
					if(options.native){
						resolve(res)	//调取接口后返回的原生数据	
					}
					if(res.statusCode === 200){
						resolve(res.data)	//异步操作执行成功
					}else{
						uni.showToast({
							title:'服务器开小差',
							icon:'none'
						})
						reject(res) 
					}
				}
			})
		})
	},
	get(url,data={},options={}){
		options.url = config.BASE_URL_LOCAL + url;
		options.data = data;
		options.method = 'get';
		options.header = {
			'content-type': 'application/json',
			'token': uni.getStorageSync('token'),
			'Accept-page-code':des.getHeader2()
		};
		return this.request(options)
	},
	post(url,data={},options={}){
		options.url = config.BASE_URL_LOCAL + url;
		options.data = data;
		options.method = 'post';
		options.header = {
			'content-type': 'application/json',
			'token': uni.getStorageSync('token'),
			'Accept-page-code':des.getHeader2()
		};
		return this.request(options)
	}
}